package com.tailyou.androidlearning202400.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.tailyou.androidlearning202400.R

class AdapterWelcome(// 列表要显示的数据，通过构造函数传递进来
    private val data: List<Int>
) : RecyclerView.Adapter<AdapterWelcome.MViewHolder>() {

    /**
     * 根据布局文件item_rv_main生成ViewHolder
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_vp_welcome, parent, false)
        return MViewHolder(view)
    }

    /**
     * 把数据绑定到布局上
     */
    override fun onBindViewHolder(holder: MViewHolder, position: Int) {
        holder.ivPic.setImageResource(data[position])
    }

    /**
     * 返回列表数据条数
     */
    override fun getItemCount(): Int {
        return data.size
    }

    /**
     * ViewHolder对item_rv_main中的组件进行封装和初始化
     */
    class MViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivPic: ImageView = itemView.findViewById(R.id.ivPic)
    }
}
